package com.wontube.smr.web.controller.admin;

import java.util.Date;
import java.util.UUID;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wontube.client.extension.utils.DateUtils;
import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.client.remote.Remoting;
import com.wontube.smr.web.client.admin.IAdminBlogHandler;
import com.wontube.smr.web.client.model.po.Blog;
import com.wontube.smr.web.client.model.po.BlogCategory;
import com.wontube.smr.web.client.model.po.BlogTag;
import com.wontube.smr.web.client.model.vo.BlogItems;
import com.wontube.web.verify.VerifyResult;

/**
 * @author tanggf(2012-5-25)
 *
 */
@Controller
@RequestMapping("/admin/blog")
public class AdminBlogController {
	private final static int BLOG_PAGE_SIZE = 10;
	private final static String SUCCESS = "Done";
	private final static String FAILURE = "Failure: something wrong.";
	
	@RequestMapping("")
	public ModelAndView admin_blog(){
		ModelAndView mv = new ModelAndView("admin/blog/blog_index");
		IAdminBlogHandler handler = getHandler();
		Object[] objs = handler.listAllBlogCategoryAndTags();		
		mv.addObject("categories", objs[0]);
		mv.addObject("tags", objs[1]);
		return mv;
	}
	
	@RequestMapping("/new")
	public ModelAndView update_index(){
		ModelAndView mv = new ModelAndView("admin/blog/new_blog");
		IAdminBlogHandler handler = getHandler();
		Object[] objs = handler.listAllBlogCategoryAndTags();		
		mv.addObject("categories", objs[0]);
		mv.addObject("tags", objs[1]);
		mv.addObject("now", DateUtils.getNow());
		return mv;
	}
	
	@RequestMapping(value="/add", method=RequestMethod.POST)
	@ResponseBody
	public String addBlog(Blog blog){
		VerifyResult verify = blog.verify();
		if( ! verify.isDone()){
			return verify.getErrorMessages();
		}
		Date date = DateUtils.parse(blog.getCreatedString());
		if(date == null){
			return "created is empty or wrong format(MM/dd/yyyy HH:mm:ss)";
		}
		
		IAdminBlogHandler handler = getHandler();
		String id = UUID.randomUUID().toString();
		blog.setId(id);		
		blog.setStatus(Blog.STATUS_DRAFT);
		blog.setCreated(date);
		boolean success = handler.addOrUpdateBlog(blog);
		if(success){
			return SUCCESS + id;
		}else{
			return FAILURE;
		}		
	}
	
	@RequestMapping("/preview/{id}")
	public ModelAndView preview_blog(@PathVariable("id") String id){
		IAdminBlogHandler handler = getHandler();
		Blog blog = handler.getBlog(id);
		ModelAndView mv = new ModelAndView("admin/blog/preview");
		mv.addObject("blog", blog);
		return mv;
	}
	
	@RequestMapping("/update/{id}")
	public ModelAndView update_blog(@PathVariable("id") String id){
		IAdminBlogHandler handler = getHandler();
		ModelAndView mv = new ModelAndView("admin/blog/blog_update");
		Object[] objs = handler.listAllBlogCategoryAndTags();		
		mv.addObject("categories", objs[0]);
		mv.addObject("tags", objs[1]);
		Blog blog = handler.getBlog(id);
		mv.addObject("blog", blog);	
		return mv;
	}
	
	@RequestMapping(value="/update", method=RequestMethod.POST)
	@ResponseBody
	public String updateBlog(Blog blog){
		VerifyResult verify = blog.verify();
		if( ! verify.isDone()){
			return verify.getErrorMessages();
		}
		Date date = DateUtils.parse(blog.getCreatedString());
		if(date == null){
			return "created is empty or wrong format(MM/dd/yyyy HH:mm:ss)";
		}
		blog.setCreated(date);
		IAdminBlogHandler handler = getHandler();		
		boolean success = handler.addOrUpdateBlog(blog);
		if(success){
			return SUCCESS;
		}else{
			return FAILURE;
		}		
	}
	
	@RequestMapping("/query")
	public ModelAndView query_blogs(@RequestParam("status") Integer status, 
			@RequestParam(value="category", required=false) String category,
			@RequestParam(value="page", required=false, defaultValue="1") Integer page){
		IAdminBlogHandler handler = getHandler();
		BlogItems blogItems = handler.listBlogs(status, category, new PagenateInfo(page, BLOG_PAGE_SIZE));
		ModelAndView mv = new ModelAndView("admin/blog/blog_list");
		mv.addObject("blogItems", blogItems);
		return mv;
	}
	
	@RequestMapping(value="/delete/{id}", method=RequestMethod.POST)
	@ResponseBody
	public String delete_blog(@PathVariable("id") String id){
		IAdminBlogHandler handler = getHandler();		
		boolean success = handler.deleteBlog(id);
		if(success){
			return SUCCESS;
		}else{
			return FAILURE;
		}		
	}
	
	@RequestMapping(value="/recall/{id}", method=RequestMethod.POST)
	@ResponseBody
	public String recall_blog(@PathVariable("id") String id){
		IAdminBlogHandler handler = getHandler();		
		boolean success = handler.recallBlog(id);
		if(success){
			return SUCCESS;
		}else{
			return FAILURE;
		}		
	}
	
	@RequestMapping(value="/cancle/{id}", method=RequestMethod.POST)
	@ResponseBody
	public String cancle_blog(@PathVariable("id") String id){
		IAdminBlogHandler handler = getHandler();		
		boolean success = handler.cancleBlog(id);
		if(success){
			return SUCCESS;
		}else{
			return FAILURE;
		}	
	}
	
	@RequestMapping(value="/release/{id}", method=RequestMethod.POST)
	@ResponseBody
	public String release_blog(@PathVariable("id") String id){
		IAdminBlogHandler handler = getHandler();		
		boolean success = handler.releaseBlog(id);
		if(success){
			return SUCCESS;
		}else{
			return FAILURE;
		}	
	}
	
	private IAdminBlogHandler getHandler(){
		Remoting<IAdminBlogHandler> remoting = new Remoting<IAdminBlogHandler>();
		return remoting.createService(IAdminBlogHandler.class);
	}
	
	@RequestMapping(value="/category/add", method=RequestMethod.POST)
	@ResponseBody
	public String add_category(BlogCategory cate){
		cate.setId(UUID.randomUUID().toString());
		IAdminBlogHandler handler = getHandler();		
		boolean success = handler.addBlogCategory(cate);
		if(success){
			return SUCCESS;
		}else{
			return FAILURE;
		}	
	}
	
	@RequestMapping(value="/category/delete/{id}", method=RequestMethod.POST)
	@ResponseBody
	public String delete_category(@PathVariable("id") String id){
		IAdminBlogHandler handler = getHandler();		
		boolean success = handler.deleteBlogCategory(id);
		if(success){
			return SUCCESS;
		}else{
			return FAILURE;
		}	
	}
	
	@RequestMapping(value="/tag/add", method=RequestMethod.POST)
	@ResponseBody
	public String add_tag(BlogTag tag){
		tag.setId(UUID.randomUUID().toString());
		IAdminBlogHandler handler = getHandler();		
		int aff = handler.addBlogTag(tag);
		if(aff == 1){
			return SUCCESS;
		}else{
			return FAILURE;
		}	
	}
	
	@RequestMapping(value="/tag/delete/{id}", method=RequestMethod.POST)
	@ResponseBody
	public String delete_tag(@PathVariable("id") String id){
		IAdminBlogHandler handler = getHandler();		
		int aff = handler.deleteBlogTag(id);
		if(aff == 1){
			return SUCCESS;
		}else{
			return FAILURE;
		}	
	}
}
